package com.iscale.service.impl;

import com.iscale.model.entity.BaseFileInfo;
import com.iscale.mapper.BaseFileInfoMapper;
import com.iscale.service.BaseFileInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.iscale.service.SysDictionaryService;
import com.iscale.util.StringUtil;
import com.iscale.util.SystemConstant;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Service
public class BaseFileInfoServiceImpl extends ServiceImpl<BaseFileInfoMapper, BaseFileInfo> implements BaseFileInfoService {

    @Autowired
    SysDictionaryService dictionaryService;

    @Override
    public BaseFileInfo saveFileByLocalServer(MultipartFile multipartFile, String rename,String merchantNo) throws IOException {
        if(null!=multipartFile){
            String realPath = dictionaryService.getSysDictionaryByKey(SystemConstant.filePath) ;//"G:\\igrowfiles";
            String baseUrl = dictionaryService.getSysDictionaryByKey(SystemConstant.baseFileUrl); //http://localhost:8080/filesever/

            if(StringUtil.isNotEmpty(merchantNo)){
                realPath = realPath + File.separator + merchantNo;
            }
            File file = new File(realPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(realPath, rename));
            //构建 文件的 web url地址  根据商户 编号 区分文件夹
            String fileUrl = "";
            if(baseUrl.endsWith(File.separator)){
                if(StringUtil.isNotEmpty(merchantNo)){
                    fileUrl = baseUrl  + merchantNo + File.separator + rename;
                }else{
                    fileUrl = baseUrl+ File.separator + rename;
                }
            }else{
                if(StringUtil.isNotEmpty(merchantNo)){
                    fileUrl = baseUrl + File.separator + merchantNo  + File.separator + rename;
                }else{
                    fileUrl = baseUrl  + File.separator + rename;
                }
            }
            //构建文件的硬盘地址
            String filePath = "";
            if(realPath.endsWith(File.separator)){
                if(StringUtil.isNotEmpty(merchantNo)){
                    filePath = realPath +  merchantNo + File.separator + rename ;
                }else{
                    filePath = realPath + rename;
                }

            }else{
                if(StringUtil.isNotEmpty(merchantNo)){
                    filePath = realPath + File.separator + merchantNo + File.separator + rename ;
                }else{
                    filePath = realPath + File.separator + rename;
                }
            }
            //构建文件信息对象 String fileType, String merchantNo, String fileName, String fileUrl, String filePath, Long fileSize, String fileFormat, String createUser
           return  new BaseFileInfo("",merchantNo,rename,fileUrl,filePath,multipartFile.getSize(),multipartFile.getContentType(),"");
        }
        return null;
    }

    @Override
    public List<BaseFileInfo> saveFileInfos(MultipartFile[] multipartFiles,String merchantNo) throws IOException {
        List<BaseFileInfo> list = null;
        if(null!=multipartFiles && multipartFiles.length>0){
            list = new ArrayList<>();
            for (MultipartFile multipartFile: multipartFiles ) {
                String rename = UUID.randomUUID().toString().replaceAll("-", "") + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
                BaseFileInfo baseFileInfo = saveFileByLocalServer(multipartFile,rename,merchantNo);
                if(null!=baseFileInfo){
                    list.add(baseFileInfo);
                }
            }
            //批量插入
            insertBatch(list);
        }
        return list;
    }


}
